package com.yuelife.service.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.pojo.SOrder;
import com.yuelife.service.pojo.SOrderItem;
import com.yuelife.service.pojo.SProduct;
import com.yuelife.service.service.SOrderItemService;
import com.yuelife.service.service.SOrderService;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "SOrderController")
@RestController
@RequestMapping("/sOrder")
@CrossOrigin
public class SOrderController {

    @Autowired
    private SOrderService sOrderService;
    @Autowired
    private SOrderItemService sOrderItemService;

    /***
     * SOrder分页条件搜索实现
     * @param sOrder
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "SOrder条件分页查询",notes = "分页条件查询SOrder方法详情",tags = {"SOrderController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "SOrder对象",value = "传入JSON数据",required = false) SOrder sOrder, @PathVariable  int page, @PathVariable  int size){
        //调用SOrderService实现分页条件查询SOrder
        PageInfo<SOrder> pageInfo = sOrderService.findPage(sOrder, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * get按条件查订单
     * @param sOrder
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/pageorder",method = RequestMethod.GET)
    public Result findPageor(SOrder sOrder, @RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "size",defaultValue = "5") int size){
        //调用SOrderService实现分页条件查询SOrder
        PageInfo<SOrder> pageInfo = sOrderService.findPage(sOrder, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 多条件查订单
     * @param sOrder
     * @return
     */
    @RequestMapping(value = "/statusorder",method = RequestMethod.GET)
    public Result findPageor(SOrder sOrder){
        //调用SOrderService实现分页条件查询SOrder
        List<SOrder> sOrders = sOrderService.selectbySOrder(sOrder);
        return new Result(true,StatusCode.OK,"查询成功",sOrders);
    }


    /***
     * SOrder分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "SOrder分页查询",notes = "分页查询SOrder方法详情",tags = {"SOrderController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SOrderService实现分页查询SOrder
        PageInfo<SOrder> pageInfo = sOrderService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param sOrder
     * @return
     */
    @ApiOperation(value = "SOrder条件查询",notes = "条件查询SOrder方法详情",tags = {"SOrderController"})
    @PostMapping(value = "/search" )
    public Result<List<SOrder>> findList(@RequestBody(required = false) @ApiParam(name = "SOrder对象",value = "传入JSON数据",required = false) SOrder sOrder){
        //调用SOrderService实现条件查询SOrder
        List<SOrder> list = sOrderService.findList(sOrder);
        return new Result<List<SOrder>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "SOrder根据ID删除",notes = "根据ID删除SOrder方法详情",tags = {"SOrderController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用SOrderService实现根据主键删除
        sOrderService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改SOrder数据
     * @param sOrder
     * @param id
     * @return
     */
    @ApiOperation(value = "SOrder根据ID修改",notes = "根据ID修改SOrder方法详情",tags = {"SOrderController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "SOrder对象",value = "传入JSON数据",required = false) SOrder sOrder, @PathVariable Integer id){
        //设置主键值
        sOrder.setOrderId(id);
        //调用SOrderService实现修改SOrder
        sOrderService.update(sOrder);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增SOrder数据
     * @param sOrder
     * @return
     */
    @ApiOperation(value = "SOrder添加",notes = "添加SOrder方法详情",tags = {"SOrderController"})
    @PostMapping(value="/addorder")
    public Result addor(SOrder sOrder,@RequestParam("allorder") String allorder){
        //调用SOrderService实现添加SOrder
        sOrder.setCreateTime(new Date());
        sOrderService.add(sOrder);
        //增加商品详情 serviceId servicePrice  promotionPrice img num
        if(StringUtils.isNotEmpty(allorder)){
            JSONArray createArray=JSONArray.parseArray(allorder);
            for(int i=0;i<createArray.size();i++){
                //增加商品详情
                SOrderItem sOrderItem = new SOrderItem();
                sOrderItem.setOrderId(sOrder.getOrderId());//订单id
                String serviceName= JSONObject.parseObject(JSONObject.toJSONString(createArray.get(i))).getString("serviceName");
                Integer serviceId =JSONObject.parseObject(JSONObject.toJSONString(createArray.get(i))).getInteger("serviceId");
                Integer num =JSONObject.parseObject(JSONObject.toJSONString(createArray.get(i))).getInteger("num");
                String servicePrice= JSONObject.parseObject(JSONObject.toJSONString(createArray.get(i))).getString("servicePrice");
                String promotionPrice= JSONObject.parseObject(JSONObject.toJSONString(createArray.get(i))).getString("promotionPrice");
                String img= JSONObject.parseObject(JSONObject.toJSONString(createArray.get(i))).getString("img");
                sOrderItem.setServiceName(serviceName);//商品名称
                sOrderItem.setImg(img);
                sOrderItem.setPromotionPrice(promotionPrice);
                sOrderItem.setServiceId(serviceId);
                sOrderItem.setServiceQuantity(num);
                sOrderItem.setServicePrice(servicePrice);
                sOrderItemService.add(sOrderItem);
               // System.out.println(sOrderItem);
            }
        }

        return new Result(true,StatusCode.OK,"添加成功",sOrder);
    }





    /***
     * 根据ID查询SOrder数据
     * @param id
     * @return
     */
    @ApiOperation(value = "SOrder根据ID查询",notes = "根据ID查询SOrder方法详情",tags = {"SOrderController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<SOrder> findById(@PathVariable Integer id){
        //调用SOrderService实现根据主键查询SOrder
        SOrder sOrder = sOrderService.findById(id);
        return new Result<SOrder>(true,StatusCode.OK,"查询成功",sOrder);
    }

    /***
     * 查询SOrder全部数据
     * @return
     */
    @ApiOperation(value = "查询所有SOrder",notes = "查询所SOrder有方法详情",tags = {"SOrderController"})
    @GetMapping
    public Result<List<SOrder>> findAll(){
        //调用SOrderService实现查询所有SOrder
        List<SOrder> list = sOrderService.findAll();
        return new Result<List<SOrder>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /**
     * 查某个店铺的所有订单
     * @param storeId
     * @return
     */
    @GetMapping("/orderitem/{storeId}")
    public Result<List<SOrder>> selectSOrderByStoreId(@PathVariable("storeId") Integer storeId){
        //调用SOrderService实现根据主键查询SOrder
        List<SOrder> sOrders = sOrderService.selectSOrderByStoreId(storeId);
        return new Result<List<SOrder>>(true,StatusCode.OK,"查询成功",sOrders);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public Result cancelOrder(Integer orderId){
        sOrderService.updateStatus(orderId);
        return new Result(true,StatusCode.OK,"取消订单成功");
    }
    /**
     * 确认收货
     * @param orderId
     * @return
     */
    @PostMapping("/confirm")
    public Result confirmOrder(Integer orderId){
        sOrderService.updateStatusConfirm(orderId);
        return new Result(true,StatusCode.OK,"收货成功");
    }



}
